
const baseUrl = 'http://192.168.1.188:8080';  
const requestP = (options = {}) => {
	console.log("执行show 1");
	uni.showLoading({title: '加载中'});
	const {
		url,
		data,
		header,
		method,
		dataType,
		responseType,
		success,
		fail,
		complete
		} = options;
     return new Promise((resolve, reject) => {
		 uni.request({
		 	url,
		 	data,
			header,
		 	method: 'POST',
			dataType,
			responseType,
		 	success: res => {
				console.log("执行hide 1");
				uni.hideLoading();
				let isSuccess = isHttpSuccess(res.statusCode);
				if(isSuccess){
					 resolve(res);
				}else{
					uni.showModal({
						content:"网络错误:" + res.errMsg
					});
				}
			},
		 	fail: (err) => {
				reject(err);
			},
		 	complete: () => {
				uni.hideLoading();
			}
		 });
    })
};

function isHttpSuccess(status) {
  return status >= 200 && status < 300 || status === 304;
}

function req(options = {}, keepLogin = true) {
  if (keepLogin) {
    return new Promise((res, rej) => {
          // 获取sessionId成功之后，发起请求
          requestP(options)
            .then((r2) => {
              if (r2.rcode === 401) {
              console.log("登陆失效");
              } else {
                res(r2);
              }
            })
            .catch((err) => {
              // 请求出错
			  console.log(err)
              rej(err);
            });
       
    });
  } else {
    // 不需要sessionId，直接发起请求
    return requestP(options);
  }
}
 
module.exports = req;
